<template>
  <div class="delivery-dashboard">
    <el-row :gutter="20">
      <!-- 统计卡片 -->
      <el-col :span="6">
        <el-card class="stat-card">
          <template #header>
            <div class="card-header">
              <span>待分配订单</span>
            </div>
          </template>
          <div class="stat-value">{{ stats.pendingOrders }}</div>
        </el-card>
      </el-col>
      <el-col :span="6">
        <el-card class="stat-card">
          <template #header>
            <div class="card-header">
              <span>配送中</span>
            </div>
          </template>
          <div class="stat-value">{{ stats.delivering }}</div>
        </el-card>
      </el-col>
      <el-col :span="6">
        <el-card class="stat-card">
          <template #header>
            <div class="card-header">
              <span>今日完成</span>
            </div>
          </template>
          <div class="stat-value">{{ stats.todayCompleted }}</div>
        </el-card>
      </el-col>
      <el-col :span="6">
        <el-card class="stat-card">
          <template #header>
            <div class="card-header">
              <span>配送员数量</span>
            </div>
          </template>
          <div class="stat-value">{{ stats.totalDeliveryStaff }}</div>
        </el-card>
      </el-col>
    </el-row>

    <!-- 待分配订单列表 -->
    <el-card class="pending-orders">
      <template #header>
        <div class="card-header">
          <span>待分配订单</span>
          <el-button type="primary" @click="handleBatchAssign">批量分配</el-button>
        </div>
      </template>
      <el-table
        :data="pendingOrders"
        style="width: 100%"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column prop="order_number" label="订单编号" width="120" />
        <el-table-column prop="customer_name" label="客户名称" width="120" />
        <el-table-column prop="address" label="配送地址" />
        <el-table-column prop="created_at" label="创建时间" width="180" />
        <el-table-column prop="priority" label="优先级" width="100">
          <template #default="{ row }">
            <el-tag :type="getPriorityType(row.priority)">
              {{ getPriorityText(row.priority) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="200">
          <template #default="{ row }">
            <el-button type="primary" link @click="handleAssign(row)">分配配送员</el-button>
            <el-button type="success" link @click="handleViewDetails(row)">查看详情</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 分配配送员对话框 -->
    <el-dialog
      v-model="assignDialogVisible"
      title="分配配送员"
      width="50%"
    >
      <el-form :model="assignForm" label-width="100px">
        <el-form-item label="配送员">
          <el-select v-model="assignForm.deliveryStaffId" placeholder="请选择配送员">
            <el-option
              v-for="staff in availableDeliveryStaff"
              :key="staff.id"
              :label="staff.name"
              :value="staff.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="备注">
          <el-input type="textarea" v-model="assignForm.note" />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="assignDialogVisible = false">取消</el-button>
          <el-button type="primary" @click="submitAssign">确认分配</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { ElMessage } from 'element-plus'

// 统计数据
const stats = ref({
  pendingOrders: 0,
  delivering: 0,
  todayCompleted: 0,
  totalDeliveryStaff: 0
})

// 待分配订单列表
const pendingOrders = ref([])
const selectedOrders = ref([])

// 分配对话框控制
const assignDialogVisible = ref(false)
const currentOrder = ref(null)

// 分配表单
const assignForm = reactive({
  deliveryStaffId: '',
  note: ''
})

// 可用配送员列表
const availableDeliveryStaff = ref([
  { id: 1, name: '配送员A', status: 'available' },
  { id: 2, name: '配送员B', status: 'available' },
  { id: 3, name: '配送员C', status: 'busy' }
])

// 获取优先级样式
const getPriorityType = (priority) => {
  const typeMap = {
    high: 'danger',
    medium: 'warning',
    low: 'info'
  }
  return typeMap[priority] || 'info'
}

// 获取优先级文本
const getPriorityText = (priority) => {
  const textMap = {
    high: '高优先级',
    medium: '中优先级',
    low: '低优先级'
  }
  return textMap[priority] || '未知'
}

// 处理选择变化
const handleSelectionChange = (selection) => {
  selectedOrders.value = selection
}

// 批量分配
const handleBatchAssign = () => {
  if (selectedOrders.value.length === 0) {
    ElMessage.warning('请选择要分配的订单')
    return
  }
  assignDialogVisible.value = true
  currentOrder.value = null
}

// 单个分配
const handleAssign = (row) => {
  currentOrder.value = row
  assignDialogVisible.value = true
}

// 查看详情
const handleViewDetails = (row) => {
  // TODO: 实现查看详情功能
  console.log('查看订单详情:', row)
}

// 提交分配
const submitAssign = async () => {
  try {
    // TODO: 调用后端 API 进行分配
    await new Promise(resolve => setTimeout(resolve, 1000))
    ElMessage.success('分配成功')
    assignDialogVisible.value = false
    fetchData() // 重新获取数据
  } catch (error) {
    console.error('分配失败:', error)
    ElMessage.error('分配失败')
  }
}

// 获取数据
const fetchData = async () => {
  try {
    // 这里应该调用后端 API 获取数据
    // 暂时使用模拟数据
    const mockOrders = [
      {
        id: 1,
        order_number: 'ORD20240301001',
        customer_name: '公司A',
        address: '北京市朝阳区建国路88号',
        created_at: '2024-03-01 09:00:00',
        priority: 'high'
      },
      {
        id: 2,
        order_number: 'ORD20240301002',
        customer_name: '公司B',
        address: '北京市海淀区中关村大街1号',
        created_at: '2024-03-01 10:30:00',
        priority: 'medium'
      },
      {
        id: 3,
        order_number: 'ORD20240301003',
        customer_name: '公司C',
        address: '北京市西城区西单大悦城',
        created_at: '2024-03-01 11:15:00',
        priority: 'low'
      }
    ]
    
    // 更新统计数据
    stats.value = {
      pendingOrders: mockOrders.length,
      delivering: 5,
      todayCompleted: 8,
      totalDeliveryStaff: availableDeliveryStaff.value.length
    }

    // 更新待分配订单列表
    pendingOrders.value = mockOrders
  } catch (error) {
    console.error('获取数据失败:', error)
  }
}

onMounted(() => {
  fetchData()
})
</script>

<style scoped>
.delivery-dashboard {
  padding: 20px;
}

.stat-card {
  margin-bottom: 20px;
}

.stat-value {
  font-size: 24px;
  font-weight: bold;
  text-align: center;
  color: #409EFF;
}

.pending-orders {
  margin-top: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.dialog-footer {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
}
</style>